﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToPi.Data;
using LinqToPi;
using System.Data.OleDb;
using System.IO;
using System.Data.Common;

namespace LinqToPi
{
    public class PiUserContext : PiContext
    {
        public IUpdatableTable<PiUser> Users { get; set; }
        public IQueryableTable<PiMembership> UserMemberships { get; set; }
        public IQueryableTable<PiGroup> UserGroups { get; set; }
   
        #region Constructors
        public PiUserContext(string servername, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ;",
                                                                            PiOleDBProvider,
                                                                            servername
                                                                          )), 
                                       log)
        {               
        }

        public PiUserContext(string servername, string username, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; ",
                                                                          PiOleDBProvider,
                                                                          servername,
                                                                          username
                                                                          )),
                                        log)
            
        {
        }

        public PiUserContext(string servername, string username, string password, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; Password = {3} ; ",
                                                                            PiOleDBProvider,
                                                                            servername,
                                                                            username,
                                                                            password
                                                                            )),
                                        log)
        {
        }

        private PiUserContext(DbConnection connection, TextWriter log)
            : this(new DbQueryProvider(connection, StandardPolicy, log))
        {
            connection.Open();
        }

        public PiUserContext(IQueryProvider provider) 
        {
            this.provider = provider;
        
           this.Users = new UpdatableTable<PiUser>(provider, "PiUser");
           this.UserGroups = new QueryableTable<PiGroup>(provider);
           this.UserMemberships = new QueryableTable<PiMembership>(provider);
       

        }

        #endregion
    }
}
